import request from './axios'

// 设备相关API
export const deviceAPI = {
  // 获取设备列表
  getDevices: (params) => {
    return request({
      url: '/device/list',
      method: 'get',
      params
    })
  },
  
  // 获取单个设备
  getDevice: (id) => {
    return request({
      url: `/device/${id}`,
      method: 'get'
    })
  },
  
  // 创建设备
  createDevice: (data) => {
    return request({
      url: '/device',
      method: 'post',
      data
    })
  },
  
  // 更新设备 - 使用设备code而不是id
  updateDevice: (code, data) => {
    return request({
      url: `/device/${code}`,
      method: 'put',
      data
    })
  },
  
  // 删除设备
  deleteDevice: (id) => {
    return request({
      url: `/device/${id}`,
      method: 'delete'
    })
  },
  
  // 切换设备状态
  toggleDeviceStatus: (deviceCode, status) => {
    return request({
      url: `/device/${deviceCode}/updateStatus`,
      method: 'put',
      params: { status }
    })
  },
  
  // 获取设备属性
  getDeviceProperties: (deviceId) => {
    return request({
      url: '/productProperty/list',
      method: 'get',
      params: {
        deviceId,
        pageSize: 10,
        currentPage: 1
      }
    })
  },
  
  // 设备认证
  certifyDevice: (deviceCode, clientId, authPassword) => {
    return request({
      url: `/device/${deviceCode}/certification`,
      method: 'put',
      params: {
        clientId,
        authPassword
      }
    })
  },
  
  // 更新设备属性值（下发命令）
  updateDeviceProperty: (deviceId, propertyName, value) => {
    return request({
      url: `/devices/${deviceId}/properties/${propertyName}`,
      method: 'put',
      data: { value }
    })
  }
}

export default deviceAPI